#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <assert.h>
template<class K,class V>
struct AVLTreeNode
{
	pair<K, V> _kv;
	AVLTreeNode<K, V>* _left;
	AVLTreeNode<K, V>* _right;
	AVLTreeNode<K, V>* _parent;
	int _bf;

	AVLTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _bf(0)
	{;}
};

template<class K, class V>
class AVLTree
{
	typedef AVLTreeNode<K, V> Node;
public:
	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			return true;
		}
		AVLTreeNode<K, V>* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (kv.first < cur->_kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (kv.first > cur->_kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(kv);
		if (kv.first < parent->_kv.first)
		{
			cur = parent->_left;
		}
		else
		{
			cur = parent->_right;
		}
		
		while (parent)
		{
			if (cur = parent->_left)
			{
				parent->_bf--;
			}
			else 
			{
				parent->_bf++;
			}
			if (parent->_bf == 0)
			{
				break;
			}
			else if (parent->_bf == 1 || parent->_bf == -1)
			{
				cur = parent;
				parent = parent->_parent;
			}
			else if (parent->_bf == 2 || parent->_bf == -2)
			{
				if (parent->_bf == -2 && cur->_bf == -1)
				{
					RotateR(parent);
					parent->_bf = 0;
					cur->_bf = 0;
				}
				else if (parent->_bf == 2 && cur->_bf == 1)
				{
					RotateL(parent);
					parent->_bf = 0;
					cur->_bf = 0;
				}
				else if (parent->_bf == -2 && cur->_bf == 1)
				{
					RotateLR(parent);
				}
				else if (parent->_bf == 2 && cur->_bf == -1)
				{
					RotateRL(parent);
				}
				else
				{
					assert("error");
				}
			}
			else
			{
				return false;
			}
		}
	}
private:

	void RotateR(const Node*& parent)
	{
		Node* child = parent->_left;
		Node* childR = child->_right;

		parent->_left = childR;
		if (childR != nullptr)
		{
			childR->_parent = parent;
		}

		Node* pparent = parent->_parent;
		if (pparent == nullptr)
		{
			_root = child;
			child->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
			{
				pparent->_left = child;
			}
			else 
			{
				pparent->_right = child;
			}
			child->_parent = pparent;
		}
	}
	void RotateR(const Node*& parent)
	{
		Node* child = parent->_left;
		Node* childR = child->_right;

		parent->_left = childR;
		if (childR != nullptr)
		{
			childR->_parent = parent;
		}

		Node* pparent = parent->_parent;
		if (pparent == nullptr)
		{
			_root = child;
			child->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
			{
				pparent->_left = child;
			}
			else
			{
				pparent->_right = child;
			}
			child->_parent = pparent;
		}
	}
	void RotateL(const Node*& parent)
	{
		Node* child = parent->_right;
		Node* childL = child->_left;

		parent->_right = childL;
		if (childL != nullptr)
		{
			childL->_parent = parent;
		}

		Node* pparent = parent->_parent;
		if (pparent == nullptr)
		{
			_root = child;
			child->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
			{
				pparent->_left = child;
			}
			else
			{
				pparent->_right = child;
			}
			child->_parent = pparent;
		}
	}

	void rotateLR(const Node*& parent)
	{
		Node* child = parent->_left;
		Node* childR = cur->_right;
		int bf = childR->_bf;

		RotateL(child);
		RotateR(parent);

		if (bf == 0)
		{
			parent->_bf = 0;
			child->_bf = 0;
			childR->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 0;
			child->_bf = 1;
			childR->_bf = 0;
		}
		else if (bf == 1)
		{
			parent->_bf = -1;
			child->_bf = 0;
			childR->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}

	void rotateRL(const Node*& parent)
	{
		Node* child = parent->_right;
		Node* childL = child->_left;
		int bf = childR->_bf;

		RotateR(child);
		RotateL(parent);

		if (bf == 0)
		{
			parent->_bf = 0;
			child->_bf = 0;
			childL->_bf = 0;
		}
		else if (bf == -1)
		{
			parent->_bf = 0;
			child->_bf = 0;
			childL->_bf = -1;
		}
		else if (bf == 1)
		{
			parent->_bf = 0;
			child->_bf = 1;
			childL->_bf = 0;
		}
		else
		{
			assert(false);
		}
	}

	Node* _root = nullptr;
};